<?php 
	
namespace app\renwu\controller;
use think\Db; // 引用DB类
use think\Request;
use think\Session;

class Renwu
{

    // http://127.0.0.1:85/index.php?s=/tdled/test
    /*
    {
        "GDH": "AK1970254",
        "LSM": "002"
    }
	*/
    public function index(){
    	
    }
    
    //getWangwang=>判断输入的链接与当前商店旺旺号是否一致
    public function getWangwang($shop){
    	$select	= Db::table("shop")
    		   -> where("shop_name",$shop)
    		   -> column("shop_wangwang");
    	return json(["data"=>$select]);
    }
    
    //获取店铺旺旺号=》下拉框
    public function select($user){
    	$select	= Db::table("shop")
    		   -> where("user_name",$user)
    		   -> where("shop_isCheck","yes")
    		   -> column("shop_name");
    	
    	return json(["data"=>$select]);
    }
    
    //发布任务活动
    public function addRenwu(){
    	$renwu_shop_type		= request()->post("renwu_shop_type");
    	$renwu_xiadan			= request()->post("renwu_xiadan");
    	$renwu_good_shop		= request()->post("renwu_good_shop");
    	$renwu_url				= request()->post("renwu_url");
    	$renwu_good_titles		= request()->post("renwu_good_titles");
    	$renwu_img_url			= request()->post("renwu_img_url");
    	$renwu_good_price		= request()->post("renwu_good_price");
    	$renwu_good_payPrice	= request()->post("renwu_good_payPrice");
    	$renwu_good_searchWord	= request()->post("renwu_good_searchWord");
    	$renwu_good_money		= request()->post("renwu_good_money");
    	$renwu_yaoqiu			= request()->post("renwu_yaoqiu");
    	$user_name				= request()->post("user_name");
    	$manager				= request()->post("manager");
    	
    	$wangwang				= Db::table("shop")
    		   				   -> where("shop_name",$renwu_good_shop)
    		   				   -> field("shop_wangwang")
    		   				   -> select();

    	$money					= Db::table("balance")
    						   -> where("user_name",$user_name)
    						   -> column("balance_money");
    	//var_dump();
    	if($money[0] < $renwu_good_money){
    		return json(["msg"=>"账户余额不足，请充值","code"=>500]);
    	}else{
    		$w						= "掌柜:".$wangwang[0]["shop_wangwang"];
	    	if($manager==$w){
	    		$renwuAdd				= Db::table("renwu")
		    						   -> insert(
		    						   		[
		    						   			"renwu_shop_type"		=> $renwu_shop_type,
		    						   			"renwu_xiadan"			=> $renwu_xiadan,
		    						   			"renwu_good_shop"		=> $renwu_good_shop,
		    						   			"renwu_url"				=> $renwu_url,
		    						   			"renwu_good_titles"		=> $renwu_good_titles,
		    						   			"renwu_img_url"			=> $renwu_img_url,
		    						   			"renwu_good_price"		=> $renwu_good_price,
		    						   			"renwu_good_payPrice"	=> $renwu_good_payPrice,
		    						   			"renwu_good_searchWord"	=> $renwu_good_searchWord,
		    						   			"renwu_good_money"		=> $renwu_good_money,
		    						   			"renwu_yaoqiu"			=> $renwu_yaoqiu,
		    						   			"renwu_addTime"			=> Db::raw("now()"),
		    						   			"user_name"				=> $user_name,
		    						   			"renwu_isTake"			=> 0,
		    						   			"renwu_isDo"			=> 0,
		    						   			"renwu_isCheck"			=> 0
		    						   		]);
		    	return json(["msg"=>"添加成功","code"=>200]);
	    	}else{
	    		return json(["msg"=>"当前链接店铺与选择的所属店铺不一致","code"=>500]);
	    	}
    	}
    }
    
    //编辑任务
    public function editRenwu(){
    	$renwu_shop_type		= request()->post("renwu_shop_type");
    	$renwu_xiadan			= request()->post("renwu_xiadan");
    	$renwu_good_shop		= request()->post("renwu_good_shop");
    	$renwu_url				= request()->post("renwu_url");
    	$renwu_good_titles		= request()->post("renwu_good_titles");
    	$renwu_img_url			= request()->post("renwu_img_url");
    	$renwu_good_price		= request()->post("renwu_good_price");
    	$renwu_good_payPrice	= request()->post("renwu_good_payPrice");
    	$renwu_good_searchWord	= request()->post("renwu_good_searchWord");
    	$renwu_good_money		= request()->post("renwu_good_money");
    	$renwu_yaoqiu			= request()->post("renwu_yaoqiu");
    	$renwu_id				= request()->post("renwu_id");
    	
    	$renwuAdd				= Db::table("renwu")
    						   -> where("renwu_id",$renwu_id)
    						   -> update(
    						   		[
    						   			"renwu_shop_type"		=> $renwu_shop_type,
    						   			"renwu_xiadan"			=> $renwu_xiadan,
    						   			"renwu_good_shop"		=> $renwu_good_shop,
    						   			"renwu_url"				=> $renwu_url,
    						   			"renwu_good_titles"		=> $renwu_good_titles,
    						   			"renwu_img_url"			=> $renwu_img_url,
    						   			"renwu_good_price"		=> $renwu_good_price,
    						   			"renwu_good_payPrice"	=> $renwu_good_payPrice,
    						   			"renwu_good_searchWord"	=> $renwu_good_searchWord,
    						   			"renwu_good_money"		=> $renwu_good_money,
    						   			"renwu_yaoqiu"			=> $renwu_yaoqiu,
    						   			"renwu_addTime"			=> Db::raw("now()")
    						   		]);
    }
    
    //删除单个任务
    public function delRenwu($id){
    	
    	$isDo	= Db::table("renwu")
    		   -> where("renwu_id",$id)
    		   -> where("renwu_isDo",1)
    		   -> select();
    	//var_dump($isDo);
    	if($isDo){
    		return json(["msg"=>"任务已完成，禁止删除；请及时审核","code"=>500]);
    	}else{
    		$isDel	= Db::table("renwu")
	    		   -> where("renwu_id",$id)
	    		   -> delete();
	    		   
	    	if($isDel){
	    		return json(["msg"=>"删除成功","code"=>200]);
	    	}else{
	    		return json(["msg"=>"请重试","code"=>500]);
	    	}
    	}
    	
    }
    
    //批量删除
    public function delMoreRenwu($renwu_id){
    	foreach($renwu_id as $renwu_id){
			$del	= Db::table('renwu')
			   	   -> where('renwu_id', $renwu_id)
			   	   -> delete();
		}
    }
    
    //搜索任务
    public function renwuSearch(){
    	$key			= request()->post("key");
    	$user_name	= request()->post("user_name");
		$isRenwu		= Db::table("renwu")
						-> where("user_name",$user_name)
						-> where("renwu_good_shop|renwu_good_searchWord|renwu_good_money","like","%".$key."%")
				   		-> order("renwu_addTime desc")
				   		-> select();
		$count			= count($isRenwu);		   
		return json(["code"=>0,"msg"=>"","count"=>$count,"data"=>$isRenwu]);
    }
    
    //返回任务列表
    public function renwuList($id){
    	$role		= Db::table("login")
    			   -> where("login_name",$id)
    			   -> select();
    	if($role[0]["register_role"]==2){
    		$renwuList	= Db::table("renwu")
	    			   -> where("user_name",$id)
	    			   -> order("renwu_addtime desc")
	    			   -> select();
	    	$count		= count($renwuList);
    	}else{
    		$renwuList	= Db::table("renwu")
	    			   -> order("renwu_addtime desc")
	    			   -> select();
	    	$count		= count($renwuList);
    	}
    	
    	//var_dump($shopList);
    	return json(["code"=>0,"msg"=>"","count"=>$count,"data"=>$renwuList]);
    } 
	
	//审核任务订单=>返回审核数据
	public function checkrenwu($id){
		//var_dump($id);
		$dingdan	= Db::table("dingdan_font")
				   -> alias("d")
				   -> join("renwu r","r.renwu_id=d.renwu_id")
				   -> join("register_font res","res.register_phone=d.user_name")
				   -> where("r.renwu_id",$id)
				   -> field("d.renwu_id,r.renwu_img_url,d.dingdanhao,d.dingdan_jietu,d.user_name,d.renwu_good_price,d.shifu_jine,d.timestamp,res.user_wangwang")
				   -> select();
		return json(["dingdan"=>$dingdan]);
	}
	//审核任务订单=》开始审核
	public function doCheckRenwu(){
		$renwu_id		= request()->post("id");
		$user_name		= request()->post("user_name");
		$font_user		= request()->post("font_user");
		$renwu_check	= request()->post("renwu_check");
		$check_reason	= request()->post("check_reason");
		
		$isCheck		= "";
		$reason			= "审核通过";
		
		if($renwu_check == 1){
			$isCheck 	= 1;
		}else{
			$isCheck 	= 2;
			$reason		= $check_reason;
		}
		$check			= Db::table("renwu")
					   -> where("renwu_id",$renwu_id)
					   -> update([
					   		"renwu_isCheck"	=> $isCheck,
					   		"check_reason"	=> $reason
					   	]);
		//如果商家审核通过，更新到刷手端，同时，将商户资金扣除相应佣金给刷手
		if($check){
			$yongjin		= Db::table("renwu")
						   -> where("renwu_id",$renwu_id)
						   -> field("renwu_good_money,renwu_good_payPrice")
						   -> select();
			//将商户余额查找出来
			$left_money		= Db::table("balance")
						   -> where("user_name",$user_name)
						   -> column("balance_money");
			
			//查找刷手的佣金余额
			$left_money_ss	= Db::table("balance_font")
						   -> where("user_name", $font_user)
						   -> column("font_money");
						   //-> select();
			$money			= $left_money[0]-$yongjin[0]['renwu_good_money'];
			$money_ss		= $left_money_ss[0]+$yongjin[0]['renwu_good_money'];
			
			Db::table("balance_font")
			-> where("user_name",$font_user)
			-> update([
					"font_money"	=> $money_ss
				]);
			
			 Db::table("balance")
			-> where("user_name",$user_name)
			-> update([
					"balance_money"	=> $money
				]);
			
			//审核通过后，同步到刷手平台
			$check1		= Db::table("renwu_font")
					   -> where("renwu_id",$renwu_id)
					   -> update([
					   		"renwu_isCheck"	=> $isCheck,
					   		"check_reason"	=> $reason
					   	]);
			
			//审核通过，生成商家待转账信息表，将商品本金转给刷手
			
			//获取刷手信息
			$shuashou_info = Db::table("user_font")
						  -> where("user_name",$font_user)
						  -> field("user_wangwang,yinhangkahao,kaihuhang,name")
						  -> select();
			/*
			 *转账表包含内容
			 * 转账订单号 =》tranfer_no
			 * 商品图片=>good_img
			 * 刷手旺旺号=》user_wangwang
			 * 转账金额=》tranfer_money
			 * 提现人银行卡=》tranfer_getCardNo
			 * 提现人开户行=》tranfer_cardBank
			 * 提现人姓名=》tranfer_name
			 * 转账状态=》tranfe
			 * r_status 0:未转账 1 转账成 2转账失败
			 * 生成时间=> tranfer_time
			 * 商家账号=》 user_name
			 */ 
			//$tranfer_no		=
			$tranfer_info = [
					'user_wangwang'		=> $shuashou_info[0]['user_wangwang'],
					'tranfer_money'		=> $yongjin[0]['renwu_good_payPrice'],
					'tranfer_getCardNo'	=> $shuashou_info[0]['yinhangkahao'],
					'tranfer_cardBank'	=> $shuashou_info[0]['kaihuhang'],
					'tranfer_name'		=> $shuashou_info[0]['name'],
					'tranfer_status'	=> 0,
					'tranfer_time'		=> Db::raw('now()'),
					'user_name'			=> $user_name
				];
			
			$tranfer	= Db::table("tranfer")
					   -> insert($tranfer_info);
		}
		
		
		return json(["code"=>200,"msg"=>"审核完毕"]);
	}
	//审核任务订单=>审核完毕查看
	public function lookCheck($id){
		//var_dump($id);
		$dingdan	= Db::table("dingdan_font")
				   -> alias("d")
				   -> join("renwu r","r.renwu_id=d.renwu_id")
				   -> join("register_font res","res.register_phone=d.user_name")
				   -> where("r.renwu_id",$id)
				   -> field("r.renwu_isCheck,r.check_reason,d.renwu_id,r.renwu_img_url,d.dingdanhao,d.dingdan_jietu,d.user_name,d.renwu_good_price,d.shifu_jine,d.timestamp,res.user_wangwang")
				   -> select();
		$ok = "";
		if($dingdan[0]["renwu_isCheck"] == 1){
			$ok = "审核通过";
		}else if($dingdan[0]["renwu_isCheck"]==2){
			$ok = "审核不通过";
		}
		return json(["dingdan"=>$dingdan,"ok"=>$ok]);
	}
	//刷手接单后，商家可查看
	public function lookrenwu($id){
		$lookRenwu	= Db::table("renwu_font")
				   -> alias("f")
				   -> join("register_font r","r.register_phone=f.font_name")
				   -> where("f.renwu_id",$id)
				   -> field("r.user_wangwang,f.timestamp,f.renwu_good_shop,f.renwu_good_titles,f.renwu_img_url")
				   -> select();
		//var_dump($lookRenwu);
		return json(["lookRenwu"=>$lookRenwu]);
	}
	
	
}
